package com.cxl.demo.service;

import com.cxl.demo.mapper.BuyHistoryMapper;
import com.cxl.demo.mapper.GoodsMapper;
import com.cxl.demo.mapper.UserMapper;
import com.cxl.demo.util.StaticValue;
import com.cxl.demo.vo.BuyHistory;
import com.cxl.demo.vo.Goods;
import com.cxl.demo.vo.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.ls.LSInput;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service

public class GoodsServiceImpl implements  GoodsService{

    @Autowired
    UserMapper userMapper;
    @Autowired
    GoodsMapper goodsMapper;


    @Autowired
    BuyHistoryMapper buyHistoryMapper;

    private String Upload= StaticValue.URL;
    @Override
    public Integer addGoods(Goods goods) {
        goodsMapper.AddGoods(goods);
        return 1;
    }

    @Override
    public Pager getGoods(Map<String,Object> map) {

        Pager pager=new Pager();


        List<Goods> list=goodsMapper.findGoodsListByName(map);
        for(Goods goods:list){
            String formatName=goods.getGoodsPic();
            goods.setGoodsPic(Upload+formatName);
        }

        pager.setRows(list);
        pager.setTotal(goodsMapper.GetGoodsNum((String) map.get("keyWord")));

        return pager;



//        return goodsMapper.findGoodsList();
    }


    @Override
    public Pager getGoodsByUserId(Map<String,Object> map) {

        List<Goods> list=goodsMapper.findGoodsListByUserId(map);

        for(Goods goods:list){
            String formatName=goods.getGoodsPic();
            goods.setGoodsPic(Upload+formatName);
        }

        Pager pager=new Pager();
                pager.setRows(list);

        pager.setTotal(goodsMapper.GetUserGoodsNum((Integer) map.get("keyWord")));

        return pager;
    }

    @Override
    public Integer delGoods(Integer goodsId) {


        if(buyHistoryMapper.findGoodsBuyHistoryNum(goodsId)==0){
            goodsMapper.deleteGoods(goodsId);
            return 1;
        }else{
            return 0;
        }


    }

    @Override
    public Integer AddGoodsNumber(Integer goodsId, Integer number) {
        goodsMapper.updateGoodsAddNumber(goodsId,number);
        return 1;
    }

    @Override
    public Integer UpdateGoods(Goods goods) {
        goodsMapper.updateGoods(goods);
        return 1;
    }

    @Override
    public Goods getGoodsById(Integer goodsId) {


        return goodsMapper.findGoodsById(goodsId);
    }

    @Override
    public Object getGoodsVal(Integer userId) {


        Map<String,Object> map=new HashMap<>();
        map.put("size",goodsMapper.GetUserGoodsNum(userId));
        map.put("page",0);
        map.put("keyWord",userId);
        List<Goods> list=goodsMapper.findGoodsListByUserId(map);


        List<String> goodsName=new ArrayList<>();
        List<Integer> goodsVal=new ArrayList<>();
        List<Integer> goodsNum=new ArrayList<>();
        for(Goods goods:list){
            String name=goods.getGoodsName();
            Integer num=goods.getGoodsNumber();
            Integer val=goods.getGoodsBuyNum();
            goodsName.add(name);
            goodsVal.add(val);
            goodsNum.add(num);
        }

        Map<String,Object> map1=new HashMap<>();
        map1.put("goodsName",goodsName);
        map1.put("goodsVal",goodsVal);
        map1.put("goodsNum",goodsNum);

        System.out.println(map);
        return map1;
    }


}
